package com.wxapp.common.util.random;

import com.wxapp.common.base.util.md5.Md5Util;
import com.wxapp.common.util.check.CheckUtil;
import org.springframework.util.Assert;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author lgl
 * @Description 随机数工具类
 * @Datetime 2022/4/23 10:40
 * @Modified By
 */
public class RandomUtil {
    private final static String BASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    private final static String SPECIALISATIONS ="~!@#$%^&*(){}[]<>?";

    private RandomUtil() {
    }

    public static Integer getRandomNumber(Integer several) {
        return ThreadLocalRandom.current().nextInt(several);
    }

    public static String getAllRandom(Integer length) {
        return getRandom(length, BASE + SPECIALISATIONS);
    }
    public static String getCommonRandom(Integer length) {
        return getRandom(length, BASE);
    }

    private static String getRandom(Integer length,String range){
        Assert.notNull(length,"没有具体长度");
        if (CheckUtil.isNullString(range)) {
            return "";
        } else {
            StringBuilder sb = new StringBuilder(length);
            if (length < 1) {
                length = 1;
            }
            int baseLength = range.length();
            for (int i = 0; i < length; ++i) {
                int number = ThreadLocalRandom.current().nextInt(baseLength);
                sb.append(range.charAt(number));
            }
            return sb.toString();
        }
    }
    /**
     * 获取一个UUID值
     *
     * @return UUID值[String]
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 获取密码
     */
    public static String md5BySalt(String str, String salt){
        return Md5Util.md5(Md5Util.md5(str) + Md5Util.md5(salt));
    }
}

